<!-- servicePromise 服务承诺-->
<template>
  <div class="content">
    <div class="content-btn">
      <Cbutton @click="handleAdd">新建服务承诺</Cbutton>
    </div>
    <el-table :data="tableList" v-loading="tableLoading" ref="tableData" border fit>
      <el-table-column type="index" label="序号" align="center" width="70"></el-table-column>
      <el-table-column prop="name" label="承诺名称" align="center"></el-table-column>
      <el-table-column prop="descrition" label="承诺内容" align="center"></el-table-column>
      <el-table-column prop="area" label="创建时间" align="center">
        <template slot-scope="scope">{{scope.row.createTime | glbTimeFormat}}</template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center">
        <template slot-scope="scope">
          <Cbutton type="text" @click="handleEdit(scope.row)">编辑</Cbutton>
          <Cbutton type="text" color="danger" @click="handleRemove(scope.row)">删除</Cbutton>
        </template>
      </el-table-column>
    </el-table>

    <Pagination @pageChange="getPage" :pageDefultData="{
        pageNum: query.pageNum,
        pageRow: query.pageSize,
        totalCount: totalCount }">
    </Pagination>

    <!-- 新建服务承诺 修改服务承诺 -->
    <el-dialog :title="dialog.title + '服务承诺'" :visible.sync="dialog.mVisible" v-if="dialog.mVisible" width="500px"
               class="dialog-form">
      <el-form :model="dialog.data" :rules="dialog.rules" ref="DlalogForm" size="medium" label-width="130px">
        <el-form-item label="服务承诺名称：" prop="name">
          <el-input v-model="dialog.data.name" placeholder="请输入服务承诺名称"></el-input>
        </el-form-item>

        <el-form-item label="承诺内容：" prop="descrition">
          <el-input type="textarea" :rows="3" v-model="dialog.data.descrition" placeholder="请输入承诺内容"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="medium" @click="dialog.mVisible = false">取 消</el-button>
        <el-button size="medium" :loading="dialog.loading" type="primary" @click="handleDiaSub('DlalogForm')">确
          定</el-button>
      </span>
    </el-dialog>
    <!-- 新建服务承诺 修改服务承诺 -->

  </div>
</template>

<script>
import { GetServicePromiseList, AddServicePromise, EditServicePromise, RemoveServicePromise } from '@/api/onlineMan'
import { servicePromiseRules } from '@/utils/rules'
export default {
  name: 'servicePromise',
  components: {},
  data () {
    return {
      query: {
        relationType: 2,
        type: 22,
        pageNum: 1,
        pageSize: 10
      },
      totalCount: 0,
      tableList: [],
      tableLoading: false,

      dialog: {
        title: '',
        mVisible: false,
        loading: false,
        oprType: 'ADD',
        data: {
          relationType: '2',
          name: '',
          type: '22',
          descrition: ''
        },
        rules: servicePromiseRules
      }
    }
  },
  props: {},
  watch: {},
  computed: {},
  mounted () {
    this.getList()
  },
  methods: {
    getList () {
      this.tableLoading = true
      let params = JSON.parse(JSON.stringify(this.query))
      GetServicePromiseList(params).then(res => {
        const data = res.data
        this.tableList = data.list
        this.totalCount = data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    handleEdit (row) {
      this.dialog.title = '修改'
      this.dialog.mVisible = true
      this.dialog.oprType = 'EDIT'
      this.dialog.data.id = row.id
      for (const key in this.dialog.data) {
        if (key === 'roleId') {
          this.dialog.data.roleId = row.roleId.toString()
        } else {
          this.dialog.data[key] = row[key]
        }
      }
    },
    handleAdd () {
      this.dialog.title = '创建'
      this.dialog.mVisible = true
      this.dialog.oprType = 'ADD'
      for (const key in this.dialog.data) {
        this.dialog.data[key] = ''
      }
      this.dialog.data.type = '22'
      this.dialog.data.relationType = '2'
      if (this.dialog.data.id) {
        delete this.dialog.data.id
      }
    },
    handleDiaSub (formName) {
      this.$refs[formName].validate(valid => {
        if (!valid) return false
        this.dialog.loading = true
        if (this.dialog.oprType === 'ADD') {
          const parmas = JSON.parse(JSON.stringify(this.dialog.data))
          AddServicePromise(parmas).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
          }).catch(() => {
            this.dialog.loading = false
          })
        }
        if (this.dialog.oprType === 'EDIT') {
          const parmas = JSON.parse(JSON.stringify(this.dialog.data))
          EditServicePromise(parmas).then(res => {
            this.$message.success(res.message || '操作成功')
            this.getList()
            this.dialog.loading = false
            this.dialog.mVisible = false
          }).catch(() => {
            this.dialog.loading = false
          })
        }
      })
    },
    handleRemove (row) {
      this.$confirm(`确定要删除该服务承诺吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        RemoveServicePromise({ id: row.id }).then(res => {
          this.getList()
          this.$message.success(res.message || '操作成功')
        }).catch(() => { })
      }).catch(() => { })
    },
    getPage (val) {
      this.query.pageNum = val.pageNum
      this.query.pageSize = val.pageRow
      this.getList()
    }
  }
}

</script>
<style lang='scss' scoped>
</style>
